﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.HtmlFunc;
using Kooboo.HtmlFunc.Providers;

namespace Kooboo.CMS.SiteConverter.Parser.Razor
{
    public class PageSettingParser : ParserBase, IParser<IPageSetting>
    {
        public static void FillResult(ParserContext context, IPageSetting obj)
        {
            var page = context.Result.Page;
            page.Name = obj.Name;
            page.IsDefault = obj.HomePage.HasValue ? obj.HomePage.Value : false;
            page.EnableScript = true;
            page.EnableTheming = true;
            page.HtmlMeta = new Sites.Models.HtmlMeta();
            page.Layout = obj.Layout;

            if (string.IsNullOrEmpty(page.Layout))
                page.Layout = obj.Name; // default to page name

            if (!string.IsNullOrEmpty(obj.Title))
            {
                page.HtmlMeta.HtmlTitle = obj.Title;
            }
            else if (context.Items.ContainsKey("TitleKey"))
            {
                var title = context.Items["TitleKey"] as Kooboo.HtmlFunc.Providers.ITitleTag;
                page.HtmlMeta.HtmlTitle = (title.Node.InnerText ?? string.Empty).Trim();
                title.Node.RemoveWithPrevWhitespace();
            }
            else
            {
                page.HtmlMeta.HtmlTitle = page.Name;
            }

            Func<int> DefaultPageOrder = () =>
            {
                var order = -1;
                var key = "DefaultPageOrder";
                if (context.Items.ContainsKey(key))
                    order = (int)context.Items[key];
                order++;
                context.Items[key] = order;
                return order;
            };

            page.Navigation = new Kooboo.CMS.Sites.Models.Navigation()
            {
                Show = obj.ShowMenu.HasValue ? obj.ShowMenu.Value : true,
                Order = obj.MenuIndex.HasValue ? obj.MenuIndex.Value : DefaultPageOrder(),
                DisplayText = page.Name
            };

            var currentAnalyseResult = context.Items[ParserUtility.CurrentAnalyseResultKey] as IAnalyseResult;
            if (currentAnalyseResult.Details.Count > 0)
            {
                page.Navigation.Show = false;
                //page.PageType = Kooboo.CMS.Sites.Models.PageType.Dynamic;
            }

            if (ParserSetting.RemoveFuncMarkers)
            {
                if (obj.TitleFuncNode != null)
                {
                    obj.TitleFuncNode.RemoveWithPrevWhitespace();
                }
                if (obj.HomePageFuncNode != null)
                {
                    obj.HomePageFuncNode.RemoveWithPrevWhitespace();
                }
                if (obj.ParentFuncNode != null)
                {
                    obj.ParentFuncNode.RemoveWithPrevWhitespace();
                }
                if (obj.LayoutFuncNode != null)
                {
                    obj.LayoutFuncNode.RemoveWithPrevWhitespace();
                }
                if (obj.ShowMenuFuncNode != null)
                {
                    obj.ShowMenuFuncNode.RemoveWithPrevWhitespace();
                }
                if (obj.MenuIndexFuncNode != null)
                {
                    obj.MenuIndexFuncNode.RemoveWithPrevWhitespace();
                }
            }
        }

        #region IParser<IPageSetting> Members

        public void Parse(IPageSetting obj)
        {
            FillResult(this.Context, obj);
        }

        #endregion
    }
}
